package com.ou.csbs.capstone.models.shapes.control;

import java.net.URL;

import com.ou.csbs.capstone.models.RenderableModel;

import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.render.BasicWWTexture;
import gov.nasa.worldwind.render.Material;
import gov.nasa.worldwind.render.Offset;
import gov.nasa.worldwind.render.PointPlacemark;
import gov.nasa.worldwind.render.PointPlacemarkAttributes;
import gov.nasa.worldwind.render.WWTexture;

/**
 * This class defines a control point object. These are displayed whenever a shape is drawn, at whatever the required coordinates are for controlling the shape, path, or location of an object.
 * In conjunction with the ControlPointPlacemark, there are a set of static attributes that define how the placemark is drawn. It is set to replicate the attributes of control points from
 * the OpenMap project.
 * 
 * @author Senior Capstone
 */
public class ControlPoint {
	private ControlPointPlacemark point;
	private PointPlacemarkAttributes placemarkAttr = new PointPlacemarkAttributes();
	
	public ControlPoint(Position position){
		this.point = new ControlPointPlacemark(position);
		this.point.setAltitudeMode(WorldWind.CLAMP_TO_GROUND);
		placemarkAttr.setImageAddress(this.getClass().getResource("ControlPoint.png").toExternalForm());
		placemarkAttr.setLineMaterial(Material.WHITE);
		placemarkAttr.setUsePointAsDefaultImage(true);
		placemarkAttr.setScale(.5d);
		placemarkAttr.setImageOffset(new Offset(0.5, 0.5, AVKey.FRACTION, AVKey.FRACTION));
        this.point.setAttributes(placemarkAttr);
	}
	
	public void drawPoint(RenderableModel model){
		model.getDrawingLayer().addRenderable(point);
	}
	
	public void removePoint(RenderableModel model){
		model.getDrawingLayer().removeRenderable(point);
	}
	
	public void updateLocation(Position position){
		this.point.setPosition((Position) position);
	}
	
	public ControlPointPlacemark getControlPointPlacemark(){
		return this.point;
	}
	/**
	 * This class is an override of the PointPlacemark, so that the Editing tool can distinguish between a Control point and other PointPlacemarks.
	 * @author bateale
	 */
	public class ControlPointPlacemark extends PointPlacemark{
		public ControlPointPlacemark(Position arg0) {
			super(arg0);
		}
		/**
		 * This is an override that removes address checks during texture initialization.
		 * 
		 * This is required to use resources internal to a jar file, which is where the ControlPoint texture is kept.
		 */
		@Override
	    protected WWTexture initializeTexture(String address) {
	        URL localUrl = WorldWind.getDataFileStore().requestFile(address);
	        return localUrl == null ? null : new BasicWWTexture(localUrl, true);
	    }
	}	 
}
